package com.qiangqiang.task.server.mapper.provider;

import com.qiangqiang.task.server.entity.Task;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class TaskMapperProvider extends BaseProvider {

    public String insertTask(final Map<String, Object> map) {
        Task task = (Task) map.get("task");
        return generateInsertSQL(Task.class);
    }

    public String queryTaskByStatus(final Map<String, Object> map) {
        final int page = (int) map.get("page");
        final int pageSize = (int) map.get("pageSize");

        SQL sql = new SQL() {
            {
                SELECT("*");
                FROM("task");
                WHERE("status = 0 and end_time > " + NOW_TIME);
                ORDER_BY("create_time desc ");
            }
        };
        StringBuffer sb = new StringBuffer(sql.toString());
        sb.append(getLimit(page, pageSize));
        return sb.toString();
    }

    public String queryAllByUserId(final Map<String, Object> map) {
        final int page = (int) map.get("page");
        final int pageSize = (int) map.get("pageSize");
        final long userId = (long) map.get("userId");
        final boolean isReceviedId = (boolean) map.get("isReceviedId");

        SQL sql = new SQL() {
            {
                SELECT("*");
                FROM("task");
                if (isReceviedId) {
                    WHERE("receiver_id=#{userId}");
                    ORDER_BY("status ");
                } else {
                    WHERE("user_id=#{userId}");
                    // 发布页面排序，未完结>未领取>已完结
                    ORDER_BY("field(status,1,0,2)");
                }
            }
        };
        StringBuffer sb = new StringBuffer(sql.toString());
        // 分页处理
//        sb.append(getLimit(page, pageSize));
//        System.out.println(sb);
        return sb.toString();
    }

}
